﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlxRRSReporter.Utils;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using GlxRRSDB.Data;
using GlxRRSDB.Utils;

namespace GlxRRSReporter.DailyReporter
{
    public class MonthlyReporter
    {
        private const string SHEET_MR_BY_CINEMA = "MR by Cinema";
        private const string SHEET_1 = "Sheet1";
        private const string SHEET_3 = "Sheet3";

        private const int REPORT_SHEET_INDEX = 1;
        private const int DATA_SHEET_INDEX = 2;
        private static string TEMPLATE_MONTHLY_NAME = ".\\templates\\MonthlyReportByCinemaTemplate.xls";
        private static string OUT_MONTHLY_NAME = "";

        public static void init(string template, string outNameVal)
        {
            TEMPLATE_MONTHLY_NAME = template;
            OUT_MONTHLY_NAME = outNameVal;
        }

        public static void generateMonthlyReportByCinema(int year, int cinemaId, string cinemaName,
            Dictionary<int, Dictionary<int, MonthlyReportData>> dictDetailBO,
            Dictionary<int, Dictionary<int, MonthlyReportData>> dictDetailCO,
            Dictionary<int, Dictionary<int, MonthlyReportData>> dictDetailAdmission)
        {
            // load excel file
            Excel.Workbook workbook = ExportToExcel.openExcelWorkbooks(TEMPLATE_MONTHLY_NAME);
            //
            Excel.Worksheet sheet;

            #region SHEET MR by Cinema
            sheet = (Excel.Worksheet)workbook.Worksheets[SHEET_MR_BY_CINEMA];
            sheet.get_Range("F3").Formula = cinemaName;
            #endregion

            #region SHEET Sheet 1
            sheet = (Excel.Worksheet)workbook.Worksheets[SHEET_1];
            //
            string header = "A";
            int beginRowBO = 4;
            string[] headerBOText = new string[] { "BO", "BO", "Target BO" };
            int beginRowCO = 12;
            string[] headerCOText = new string[] { "CO", "CO", "Target CO" };
            int beginRowAdmission = 19;
            string[] headerAdmissionText = new string[] { "Admission", "Admission" };
            for (int i = 0; i < headerBOText.Count(); i++)
            {
                string addr = header + (beginRowBO + i).ToString();
                string val = headerBOText[i] + " " + (year - i % 2);
                sheet.get_Range(addr).Formula = val;
            }
            for (int i = 0; i < headerCOText.Count(); i++)
            {
                string addr = header + (beginRowCO + i).ToString();
                string val = headerCOText[i] + " " + (year - i % 2);
                sheet.get_Range(addr).Formula = val;
            }
            for (int i = 0; i < headerAdmissionText.Count(); i++)
            {
                string addr = header + (beginRowAdmission + i).ToString();
                string val = headerAdmissionText[i] + " " + (year - i % 2);
                sheet.get_Range(addr).Formula = val;
            }
            //
            string[] column = new string[] { "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" };
            string[] rowBO = new string[] { "4", "5", "6" };
            string[] rowCO = new string[] { "12", "13", "14" };
            string[] rowAdmission = new string[] { "19", "20", "21" };

            #region CHART BO
            int rowIdx = -1;
            int columnIdx = -1;
            foreach (Dictionary<int, MonthlyReportData> dict in dictDetailBO.Values)
            {
                rowIdx++;
                foreach (MonthlyReportData detail in dict.Values)
                {
                    columnIdx = detail.month - 1;
                    string addr = column[columnIdx] + rowBO[rowIdx];
                    sheet.get_Range(addr).Formula = detail.value;
                }
            }
            #endregion

            #region CHART CO
            rowIdx = -1;
            columnIdx = -1;
            foreach (Dictionary<int, MonthlyReportData> dict in dictDetailCO.Values)
            {
                rowIdx++;
                foreach (MonthlyReportData detail in dict.Values)
                {
                    columnIdx = detail.month - 1;
                    string addr = column[columnIdx] + rowCO[rowIdx];
                    sheet.get_Range(addr).Formula = detail.value;
                }
            }
            #endregion

            #region CHART ADMISSION
            rowIdx = -1;
            columnIdx = -1;
            foreach (Dictionary<int, MonthlyReportData> dict in dictDetailAdmission.Values)
            {
                rowIdx++;
                foreach (MonthlyReportData detail in dict.Values)
                {
                    columnIdx = detail.month - 1;
                    string addr = column[columnIdx] + rowAdmission[rowIdx];
                    sheet.get_Range(addr).Formula = detail.value;
                }
            }
            #endregion

            #endregion
            // save
            workbook.SaveAs(OUT_MONTHLY_NAME);
        }
    }
}
